package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.core.util.StringUtils;
import com.ly.mpayment.persist.service.uporder.IUporderService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import net.sf.json.JSONObject;

@Component
@Path("system/profit/")
public class ProfitRest extends AbsRest{

	@Autowired
	IUserService userService;

	@Autowired
	IUporderService orderService;


	@Path("countProfit")
	@POST
	public String countProfit(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "start","pageSize","starttime","endtime","useruuid","provinceuuid","busuuid","facevalue");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("starttime"))){
				parameters.put(":starttime", json.getString("starttime"));
			}
			if(!StringUtils.isBlank(json.getString("endtime"))){
				parameters.put(":endtime", json.getString("endtime"));
			}
			if(!StringUtils.isBlank(json.getString("useruuid"))){
				parameters.put(":useruuid", json.getString("useruuid"));
			}
			if(!StringUtils.isBlank(json.getString("provinceuuid"))){
				parameters.put(":provinceuuid", json.getString("provinceuuid"));
			}
			if(!StringUtils.isBlank(json.getString("busuuid"))){
				parameters.put(":busuuid", json.getString("busuuid"));
			}
			if(!StringUtils.isBlank(json.getString("starttime"))){
				parameters.put(":facevalue", json.getString("facevalue"));
			}
			return ResultMessage.getSucceed( JsonUtils.convertObjectToJson(this.orderService.countProfit(parameters,json.getInt("start"),json.getInt("pageSize"))));
		}
		return result;
	}

	@Path("countDownProfit")
	@POST
	public String countDownProfit(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "start","pageSize","starttime","endtime","useruuid","downprouuid","busuuid","facevalue");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("starttime"))){
				parameters.put(":starttime", json.getString("starttime"));
			}
			if(!StringUtils.isBlank(json.getString("endtime"))){
				parameters.put(":endtime", json.getString("endtime"));
			}
			if(!StringUtils.isBlank(json.getString("useruuid"))){
				parameters.put(":useruuid", json.getString("useruuid"));
			}
			if(!StringUtils.isBlank(json.getString("downprouuid"))){
				parameters.put(":downprouuid", json.getString("downprouuid"));
			}
			if(!StringUtils.isBlank(json.getString("busuuid"))){
				parameters.put(":busuuid", json.getString("busuuid"));
			}
			if(!StringUtils.isBlank(json.getString("starttime"))){
				parameters.put(":facevalue", json.getString("facevalue"));
			}
			return ResultMessage.getSucceed( this.orderService.countDownProfit(parameters,json.getInt("start"),json.getInt("pageSize")));
		}
		return result;
	}

	@Path("countProfitRate")
	@POST
	public String countProfitRate(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "start","pageSize","starttime","endtime","useruuid",
				"provinceuuid","busuuid","facevalue","operatoruid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			Map<String, Object> parameters = MapUtil.getMap();
			if(!StringUtils.isBlank(json.getString("starttime"))){
				parameters.put(":starttime", json.getString("starttime"));
			}
			if(!StringUtils.isBlank(json.getString("endtime"))){
				parameters.put(":endtime", json.getString("endtime"));
			}
			if(!StringUtils.isBlank(json.getString("useruuid"))){
				parameters.put(":useruuid", json.getString("useruuid"));
			}
			if(!StringUtils.isBlank(json.getString("provinceuuid"))){
				parameters.put(":provinceuuid", json.getString("provinceuuid"));
			}
			if(!StringUtils.isBlank(json.getString("busuuid"))){
				parameters.put(":busuuid", json.getString("busuuid"));
			}
			if(!StringUtils.isBlank(json.getString("starttime"))){
				parameters.put(":facevalue", json.getString("facevalue"));
			}
			if(!StringUtils.isBlank(json.getString("operatoruid"))){
				parameters.put(":operatoruid", json.getString("operatoruid"));
			}
			return ResultMessage.getSucceed(this.orderService.countProfitRate(parameters,json.getInt("start"),json.getInt("pageSize")));
		}
		return result;
	}
}
